// const autojsUtils = require('./modules/autojs-utils');
// wave fcfa ui
"ui";

const Constant = require("./constant.js");

auto();
device.keepScreenOn();


// 之前保存的钱包
Constant.initMtnAndPin();
// let preWalletNumber = storages.create(Constant.WALLET_NAME).get(Constant.WALLET_NUMBER) || "";
// let preWalletPin = storages.create(Constant.WALLET_NAME).get(Constant.WALLET_PIN) || "";

ui.layout(
    <vertical>
        <text id="sceneName"
            w="*"
            h="auto"
            gravity="center"
            marginTop="16"
            textColor="red"
            textStyle="bold"
            textSize="16"
            text={Constant.WALLET_WAVE_UGX_NAME} />

        <input id="wallet"
            marginTop="16"
            hint="请输入钱包号码, 带0开头"
            text={Constant.getMtn()}
            textSize="14" />

        <input id="pin"
            marginTop="16"
            hint="请输入钱包PIN"
            text={Constant.getPin()}
            textSize="14" />

        <button id="saveAndStart"
            marginTop="16"
            text="保存钱包号并启动脚本" />

        <button id="saveOnly"
            marginTop="10"
            text="仅保存钱包号" />

    </vertical>
);

ui.saveAndStart.click(function () {
    var name = ui.wallet.text();
    var pin = ui.pin.text();

    if (!name || (name.length <= 8)) {
        // main();
        toast("saveWalletAndStart: 钱包号错误!");
        return;
    }

    if (!pin || (pin.length <= 2)) {
        // main();
        toast("saveWalletAndStart: pin错误!");
        return;
    }

    // 保存本地
    Constant.setMtn(name);
    Constant.setPin(pin);

    toast(name + "," + pin + " 已启动脚本 saveAndStart");

    setTimeout(() => {
        // 启动脚本
        threads.start(function () {
            //在新线程执行的代码
            main();
        });
    }, 2000);
});

ui.saveOnly.click(function () {
    var name = ui.wallet.text();
    var pin = ui.pin.text();

    if (!name || (name.length <= 8)) {
        // main();
        toast("saveOnly: 钱包号错误!");
        return;
    }

    if (!pin || (pin.length <= 2)) {
        // main();
        toast("saveOnly: pin错误!");
        return;
    }

    // 保存本地
    Constant.setMtn(name);
    Constant.setPin(pin);

    toast(name + "," + pin + " 已保存 saveOnly");
});


const SmsUtil = require("./baseSmsUtil.js");
const RunDefine = require("./baseRunDefine.js");

// wave
const WaveAppOpen = require("./waveAppOpen.js");
const WaveFcfaPageUtil = require("./waveFcfaPageUtil.js");
const WaveFcfaFunAction = require("./waveFcfaFunAction.js");


// 测试转账
// let testMtnTransfer = true;

// 转账信息
let targetMtnTransferAmount = "";
let targetMtnTransferId = "";
let targetMtnTransferAccount = "";
let targetMtnTransferReference = "";

const initMtnTransfer = (data) => {
    targetMtnTransferAmount = data.amount + "";
    targetMtnTransferId = data.id + "";
    targetMtnTransferAccount = data.mtn + "";
    targetMtnTransferReference = data.reference + "";
}

const isValidMtnTransfer = () => {
    let result1 = targetMtnTransferAmount || "";
    let result2 = targetMtnTransferId || "";
    let result3 = targetMtnTransferAccount || "";

    return ((result1.length > 0) && (result2.length > 0) && (result3.length > 0));
}

const cleanMtnTransfer = () => {
    targetMtnTransferAmount = "";
    targetMtnTransferId = "";
    targetMtnTransferAccount = "";
    targetMtnTransferReference = "";
}

const printMtnTransferLog = () => {
    console.log(Constant.UI_SDK_VERSION + "targetMtnTransferAmount: " + targetMtnTransferAmount + ",\ntargetMtnTransferId: " + targetMtnTransferId + ",\ntargetMtnTransferAccount: " + targetMtnTransferAccount + ",\ntargetMtnTransferReference: " + targetMtnTransferReference);
}


/** 启动脚本逻辑 */
function main() {
    // 设置坐标宽高
    setScreenMetrics(720, 1640);
    console.log("w:" + device.width + " ,h:" + device.height);

    // 程序结束自动 隐藏控制台
    console.show(false);
    // console.setMaxLines(15); --temp close

    // if (true) {
    //     WaveFcfaFunAction.doTestArray();
    //     return;
    // }

    // 是否需要重启app
    let isNeedRestartWalletApp = false;

    // 是否需要重启脚本
    let isNeedRestartScript = false;

    // 上传短信
    SmsUtil.syncSmsData();

    console.log(Constant.UI_SDK_VERSION + "当前设备: " + Constant.getMtn());
    // sleep(5 * 1000); --temp close
    WaveAppOpen.openWaveApp();

    // 上传短信 --temp close
    // SmsUtil.syncSmsData();
    // sleep(5 * 1000);

    let testTimes = 0
    while (testTimes < 2) {
        console.log("==========" + testTimes + "=========")

        // 查询当前页状态
        let pageStatus = WaveFcfaPageUtil.doCheckPageStatus();

        // 是否为首页
        let isHomePage = pageStatus.isHomePage;

        // 是否为登录页
        if (pageStatus.isLoginPage) {
            let doLoginResult = WaveFcfaFunAction.doLogin();
            if (doLoginResult) {
                // 判断是否为 首页
                pageStatus = WaveFcfaPageUtil.doCheckPageStatus();
                isHomePage = pageStatus.isHomePage;
            }
        }

        // 是否为 Send Money页
        if (pageStatus.isSendMoneyPage) {
            // 返回首页
            WaveFcfaPageUtil.doToHomePageFromSendMoneyPage();
            // 判断是否为 首页
            isHomePage = WaveFcfaPageUtil.doOpenHomePage();
        }

        // 是否为 Send Money Fail 页
        if (pageStatus.isSendMoneyFailPage) {
            // 返回首页
            WaveFcfaPageUtil.doToHomePageFromSendMoneyFailPage();
            // 判断是否为 首页
            isHomePage = WaveFcfaPageUtil.doOpenHomePage();
        }

        // 是否为 Send Money Amount Input Confirm 页
        if (pageStatus.isSendMoneyAmountInputConfirmPage) {
            // 返回首页
            WaveFcfaPageUtil.doToHomePageFromSendMoneyAmountInputConfirmPage();
            // 判断是否为 首页
            isHomePage = WaveFcfaPageUtil.doOpenHomePage();
        }

        // 是否为 Send Money Amount Input 页
        if (pageStatus.isSendMoneyAmountInputPage) {
            // 返回首页
            WaveFcfaPageUtil.doToHomePageFromSendMoneyAmountInputPage();
            // 判断是否为 首页
            isHomePage = WaveFcfaPageUtil.doOpenHomePage();
        }

        // 是否为 Bill Detail 页
        if (pageStatus.isBillDetailPage) {
            // 返回首页
            WaveFcfaPageUtil.doToHomePageFromBillDetailPage();
            // 判断是否为 首页
            isHomePage = WaveFcfaPageUtil.doOpenHomePage();
        }

        // 判断是否为首页
        if (isHomePage) {
            // 查询是否需要放款
            let isNeedTransfer = (testTimes === 1);
            if (isNeedTransfer) {
                // 去放款流程
                WaveFcfaFunAction.doSendMoney();
                sleep(1000);
            }

            // 查询每版面的数据
            WaveFcfaFunAction.doScanBillsForEachScreen();
        }

        sleep(5 * 1000)
        testTimes += 1;
    }
}



